<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <button>取消请求</button>

  <script>
    const xhr = new XMLHttpRequest()

    xhr.onload = function () {
      console.log(xhr.response)
    }

    xhr.onabort = function () {
      console.log("请求被取消")
    }

    xhr.ontimeout = function () {
      console.log("请求超时")
    }

    xhr.responseType = 'json'
    // timeout:浏览器达到了超时时间，还没有收到响应，取消本次请求
    xhr.timeout = 3000 // 默认为0:没有超时时间

    xhr.open("get", "http://123.207.32.32:1888/01_basic/timeout")

    xhr.send()

    // 2.手动取消结果
    const cancelBtn = document.querySelector("button")
    cancelBtn.onclick = function () {
      xhr.abort()
    }


  </script>

</body>

</html>